package com.microservice.salmon.web.controller;

import com.microservice.salmon.db.entities.SpecificationsItem;
import com.microservice.salmon.service.goods.specifications.SpecificationsService;
import com.salmon.common.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品规格
 * </p>
 *
 * @author 王洪庆 2019-03-15 22:44
 */
@RestController
@RequestMapping("/spec")
public class GoodsSpecifications {

    @Autowired
    private SpecificationsService specificationsService;

    @GetMapping("/group/{categoryId:\\d+}")
    public ResponseEntity<ApiResponse> queryGroupByCategoryId(@PathVariable("categoryId") Integer categoryId) {
        return ResponseEntity.ok(ApiResponse.ofSuccess(specificationsService.queryGroupByCategoryId(categoryId)));
    }

    /**
     * 根据规格组ID获取规格项(规格参数)
     * @param groupId 组ID
     * @return
     */
    @GetMapping("/param/{groupId:\\d+}")
    public ResponseEntity<ApiResponse> querySpecItemBySpecGroup(@PathVariable("groupId") Integer groupId) {
        return ResponseEntity.ok(ApiResponse.ofSuccess(specificationsService.querySpecItemBySpecGroup(groupId)));
    }

    @PostMapping("/param")
    public ResponseEntity<ApiResponse> addSpecParam(SpecificationsItem specificationsItem) {
        specificationsService.add(specificationsItem);
        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.ofSuccess(ApiResponse.ofSuccess()));
    }

    @PutMapping("/param")
    public ResponseEntity<ApiResponse> modifySpecParam(SpecificationsItem specificationsItem) {
        specificationsService.modifySpecParam(specificationsItem);
        return ResponseEntity.ok(ApiResponse.ofSuccess(ApiResponse.ofSuccess()));
    }

    @DeleteMapping("/param/{id}")
    public ResponseEntity<ApiResponse> deleteSpecParam(@PathVariable("id") Integer id) {
        specificationsService.deleteSpecParam(id);
        return ResponseEntity.ok(ApiResponse.ofSuccess(ApiResponse.ofSuccess()));
    }
}
